############
## Price of Peace Replication 2.R
## Last modified: April 20, 2019
## Josh Kertzer

## This file generates the analysis for the supplementary appendix - run Price of Replication 0.R first to generate the necessary dataframes
## All of the following analyses were carried out using R version 3.5.2 GUI 1.7.0 El Capitan build (7612) on a 2017 iMac Pro running macOS Mojave 10.14.4

############


########## Appendix §1 #########

##### Figure 1: Modeling the relationship between prior beliefs and the interpretation of evidence

#Iran simulation: prior on x axis, posterior on y axis, with constant likelihood 

P1 <- seq(0,1,by=0.1)
P0 <- 1 - P1

plot(P1, P1, type="n", xlab="Pr(Trustworthy)", ylab="Pr(Trustworthy | Signal)")

for (i in seq(0.1,0.9,by=0.05)){
	LR1 <- i
	LR0 <- 1-i
	y <- (LR1*P1)/(LR1*P1 + LR0 * P0)
	lines(P1, y)
}

text(0.19,0.71,"Pr(Signal | Trustworthy) = 0.9", srt=50, cex=0.7)
text(0.46,0.43,"Pr(Signal | Trustworthy) = 0.5", srt=43, cex=0.7)
text(0.78,0.26,"Pr(Signal | Trustworthy) = 0.1", srt=45, cex=0.7)

#Second panel, where LRs are endogenous to priors

plot(P1, y, type="n", xlab="Pr(Trustworthy)", ylab="Pr(Trustworthy | Signal)")
y <- (P1*P1)/(P1*P1 + P0 * P0)
lines(P1, y)

########## Appendix §2 #########

##### Table 1: The Effect of Costly Signals on DVs apart from Trustworthiness

mod.d1 <- lm(dangerous ~ Inspection_NotAllow + Inspection_All + FuelCycle_Continue + FuelCycle_Dismantle, data=dat1)
mod.d2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.d3 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + Male + log(age) + White + educ + pid1, data=dat1)
mod.d4 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.s1 <- lm(dealSupport ~ Inspection_NotAllow + Inspection_All + FuelCycle_Continue + FuelCycle_Dismantle, data=dat1)
mod.s2 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.s3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + Male + log(age) + White + educ + pid1, data=dat1)
mod.s4 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.l1 <- lm(dealLikely ~ Inspection_NotAllow + Inspection_All + FuelCycle_Continue + FuelCycle_Dismantle, data=dat1)
mod.l2 <- lm(dealLikely ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.l3 <- lm(dealLikely ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + Male + log(age) + White + educ + pid1, data=dat1)
mod.l4 <- lm(dealLikely ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)

stargazer(mod.d1, mod.d2, mod.d3, mod.d4, mod.s1, mod.s2, mod.s3, mod.s4, mod.l1, mod.l2, mod.l3, mod.l4,  title="The effects of costly signaling on DVs apart from trustworthiness", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:a1", covariate.labels=c("Inspections: None", "Inspections: All", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Militant Intl.", "Cooperative Intl.", "Isolationism", "Feelings toward Iran", "Need for cognition", "Male", "log(Age)", "White", "Education", "Party ID"))

##### Table 2: Results from fully-saturated three-way interaction models
mod3d.a <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All)*(FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod3d.b <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All)*(FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod3d.c <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All)*(FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
stargazer(mod3d.a, mod3d.b, mod3d.c, title="Three-way interaction models", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:3d", covariate.labels=c("Inspections: None", "Inspections: All", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Cooperative internationalism", "Militant internationalism", "Isolationism", "Male", "log(Age)", "White", "Education", "Party ID", "Inspections: None x Fuel Cycle: Continue", "Inspections: None: Fuel Cycle: Dismantle", "Inspections: All x Fuel Cycle: Continue", "Inspections: All x Fuel Cycle: Dismantle", "Inspections: None x CI", "Inspections: All x CI", "Fuel Cycle: Continue x CI", "Fuel Cycle: Dismantle x CI", "Inspections: None x Fuel Cycle: Continue x CI", "Inspections: None x Fuel Cycle: Dismantle x CI", "Inspections: All x Fuel Cycle: Continue x CI", "Inspections: All x Fuel Cycle: Dismantle x CI", "Constant"), dep.var.labels.include=FALSE, column.labels=c("Trustworthy", "Dangerous", "Approval of Deal"))

##### Figure 2: Results from fully-saturated three-way interaction models
###### Panel a: Conditional effect of complete inspections on CI and fuel cycle

set.seed(43215)
B <- 1500

ci3.mat1a <- matrix(NA, nrow=B,ncol=6)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*(FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=temp)
	ci3.mat1a[j,] <- coef(temp.mod)[c(3,16,17,19,24,25)]
}

ci3.mat2a <- matrix(NA, nrow=B,ncol=6)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*(FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=temp)
	ci3.mat2a[j,] <- coef(temp.mod)[c(3,16,17,19,24,25)]
}

ci3.mat3a <- matrix(NA, nrow=B,ncol=6)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*(FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=temp)
	ci3.mat3a[j,] <- coef(temp.mod)[c(3,16,17,19,24,25)]
}

### Trustworthiness
c.val <- seq(0,1,length.out=11)
ci.plot1a <- matrix(NA, 11,3) #Fuel cycle = continue
for (i in 1:11){
	ci.plot1a[i,1] <- mean(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*0)
	ci.plot1a[i,2] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*0, 0.025)
	ci.plot1a[i,3] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*0, 0.975)  
}

ci.plot1b <- matrix(NA, 11,3) #Fuel cycle = freeze
for (i in 1:11){
	ci.plot1b[i,1] <- mean(ci3.mat1a[,1] + ci3.mat1a[,2]*1 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*1 + ci3.mat1a[,6]*c.val[i]*0)
	ci.plot1b[i,2] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*1 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*1 + ci3.mat1a[,6]*c.val[i]*0, 0.025)
	ci.plot1b[i,3] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*1 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*1 + ci3.mat1a[,6]*c.val[i]*0, 0.975)  
}

ci.plot1c <- matrix(NA, 11,3) #Fuel cycle = dismantle
for (i in 1:11){
	ci.plot1c[i,1] <- mean(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*1 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*1)
	ci.plot1c[i,2] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*1 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*1, 0.025)
	ci.plot1c[i,3] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*1 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*1, 0.975)  
}

### Dangerousness
ci.plot2a <- matrix(NA, 11,3) #Fuel cycle = continue
for (i in 1:11){
	ci.plot2a[i,1] <- mean(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*0)
	ci.plot2a[i,2] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*0, 0.025)
	ci.plot2a[i,3] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*0, 0.975)  
}

ci.plot2b <- matrix(NA, 11,3) #Fuel cycle = freeze
for (i in 1:11){
	ci.plot2b[i,1] <- mean(ci3.mat2a[,1] + ci3.mat2a[,2]*1 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*1 + ci3.mat2a[,6]*c.val[i]*0)
	ci.plot2b[i,2] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*1 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*1 + ci3.mat2a[,6]*c.val[i]*0, 0.025)
	ci.plot2b[i,3] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*1 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*1 + ci3.mat2a[,6]*c.val[i]*0, 0.975)  
}

ci.plot2c <- matrix(NA, 11,3) #Fuel cycle = dismantle
for (i in 1:11){
	ci.plot2c[i,1] <- mean(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*1 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*1)
	ci.plot2c[i,2] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*1 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*1, 0.025)
	ci.plot2c[i,3] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*1 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*1, 0.975)  
}

### Support
ci.plot3a <- matrix(NA, 11,3) #Fuel cycle = continue
for (i in 1:11){
	ci.plot3a[i,1] <- mean(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*0)
	ci.plot3a[i,2] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*0, 0.025)
	ci.plot3a[i,3] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*0, 0.975)  
}

ci.plot3b <- matrix(NA, 11,3) #Fuel cycle = freeze
for (i in 1:11){
	ci.plot3b[i,1] <- mean(ci3.mat3a[,1] + ci3.mat3a[,2]*1 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*1 + ci3.mat3a[,6]*c.val[i]*0)
	ci.plot3b[i,2] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*1 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*1 + ci3.mat3a[,6]*c.val[i]*0, 0.025)
	ci.plot3b[i,3] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*1 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*1 + ci3.mat3a[,6]*c.val[i]*0, 0.975)  
}

ci.plot3c <- matrix(NA, 11,3) #Fuel cycle = dismantle
for (i in 1:11){
	ci.plot3c[i,1] <- mean(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*1 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*1)
	ci.plot3c[i,2] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*1 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*1, 0.025)
	ci.plot3c[i,3] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*1 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*1, 0.975)  
}

out.mat <- data.frame(CI=rep(c.val,9), rbind(ci.plot1a, ci.plot1b, ci.plot1c, ci.plot2a, ci.plot2b, ci.plot2c, ci.plot3a, ci.plot3b, ci.plot3c))
colnames(out.mat) <- c("CI", "Y", "Low", "High")
out.mat$DV <- rep(c("A. Trustworthiness of Iran", "B. Danger posed by Iran", "C. Support for deal"), each=3*length(c.val))
out.mat$Mod <- rep(rep(c("Fuel Cycle: Continue", "Fuel Cycle: Freeze", "Fuel Cycle: Dismantle"), each=length(c.val)),3)

dev.new(height=7, width=7.2)
ggplot(out.mat, aes(x=CI, y=Y)) + geom_ribbon(aes(ymin=Low, ymax=High), fill="grey70", alpha=0.5) + geom_line(aes(x=CI, y=Y))  + geom_hline(yintercept=0, linetype=3, alpha=0.5) +facet_grid(DV ~ Mod) + labs(x="Cooperative internationalism", y="Conditional effect of complete inspections") + theme_bw()


###### Panel b: Conditional effect of dismantling fuel cycle on CI and inspections

set.seed(43215)
B <- 1500

ci3.mat1a <- matrix(NA, nrow=B,ncol=6)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*(FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=temp)
	ci3.mat1a[j,] <- coef(temp.mod)[c(5,15,17,21,23,25)]
}

ci3.mat2a <- matrix(NA, nrow=B,ncol=6)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*(FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=temp)
	ci3.mat2a[j,] <- coef(temp.mod)[c(5,15,17,21,23,25)]
}

ci3.mat3a <- matrix(NA, nrow=B,ncol=6)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*(FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=temp)
	ci3.mat3a[j,] <- coef(temp.mod)[c(5,15,17,21,23,25)]
}

### Trustworthiness
c.val <- seq(0,1,length.out=11)
ci.plot1a <- matrix(NA, 11,3) #Inspections = none
for (i in 1:11){
	ci.plot1a[i,1] <- mean(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*0)
	ci.plot1a[i,2] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*0, 0.025)
	ci.plot1a[i,3] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*0, 0.975)  
}

ci.plot1b <- matrix(NA, 11,3) #Inspections = civilian only
for (i in 1:11){
	ci.plot1b[i,1] <- mean(ci3.mat1a[,1] + ci3.mat1a[,2]*1 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*1 + ci3.mat1a[,6]*c.val[i]*0)
	ci.plot1b[i,2] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*1 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*1 + ci3.mat1a[,6]*c.val[i]*0, 0.025)
	ci.plot1b[i,3] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*1 + ci3.mat1a[,3]*0 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*1 + ci3.mat1a[,6]*c.val[i]*0, 0.975)  
}

ci.plot1c <- matrix(NA, 11,3) #Inspections = all
for (i in 1:11){
	ci.plot1c[i,1] <- mean(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*1 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*1)
	ci.plot1c[i,2] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*1 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*1, 0.025)
	ci.plot1c[i,3] <- quantile(ci3.mat1a[,1] + ci3.mat1a[,2]*0 + ci3.mat1a[,3]*1 + c.val[i]*ci3.mat1a[,4] + ci3.mat1a[,5]*c.val[i]*0 + ci3.mat1a[,6]*c.val[i]*1, 0.975)  
}

###Dangerousness
ci.plot2a <- matrix(NA, 11,3) #Inspections = none
for (i in 1:11){
	ci.plot2a[i,1] <- mean(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*0)
	ci.plot2a[i,2] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*0, 0.025)
	ci.plot2a[i,3] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*0, 0.975)  
}

ci.plot2b <- matrix(NA, 11,3) #Inspections = civilian only
for (i in 1:11){
	ci.plot2b[i,1] <- mean(ci3.mat2a[,1] + ci3.mat2a[,2]*1 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*1 + ci3.mat2a[,6]*c.val[i]*0)
	ci.plot2b[i,2] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*1 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*1 + ci3.mat2a[,6]*c.val[i]*0, 0.025)
	ci.plot2b[i,3] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*1 + ci3.mat2a[,3]*0 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*1 + ci3.mat2a[,6]*c.val[i]*0, 0.975)  
}

ci.plot2c <- matrix(NA, 11,3) #Inspections = all
for (i in 1:11){
	ci.plot2c[i,1] <- mean(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*1 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*1)
	ci.plot2c[i,2] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*1 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*1, 0.025)
	ci.plot2c[i,3] <- quantile(ci3.mat2a[,1] + ci3.mat2a[,2]*0 + ci3.mat2a[,3]*1 + c.val[i]*ci3.mat2a[,4] + ci3.mat2a[,5]*c.val[i]*0 + ci3.mat2a[,6]*c.val[i]*1, 0.975)  
}

###Support
ci.plot3a <- matrix(NA, 11,3) #Inspections = none
for (i in 1:11){
	ci.plot3a[i,1] <- mean(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*0)
	ci.plot3a[i,2] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*0, 0.025)
	ci.plot3a[i,3] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*0, 0.975)  
}

ci.plot3b <- matrix(NA, 11,3) #Inspections = civilian only
for (i in 1:11){
	ci.plot3b[i,1] <- mean(ci3.mat3a[,1] + ci3.mat3a[,2]*1 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*1 + ci3.mat3a[,6]*c.val[i]*0)
	ci.plot3b[i,2] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*1 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*1 + ci3.mat3a[,6]*c.val[i]*0, 0.025)
	ci.plot3b[i,3] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*1 + ci3.mat3a[,3]*0 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*1 + ci3.mat3a[,6]*c.val[i]*0, 0.975)  
}

ci.plot3c <- matrix(NA, 11,3) #Inspections = all
for (i in 1:11){
	ci.plot3c[i,1] <- mean(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*1 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*1)
	ci.plot3c[i,2] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*1 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*1, 0.025)
	ci.plot3c[i,3] <- quantile(ci3.mat3a[,1] + ci3.mat3a[,2]*0 + ci3.mat3a[,3]*1 + c.val[i]*ci3.mat3a[,4] + ci3.mat3a[,5]*c.val[i]*0 + ci3.mat3a[,6]*c.val[i]*1, 0.975)  
}

out.mat <- data.frame(CI=rep(c.val,9), rbind(ci.plot1a, ci.plot1b, ci.plot1c, ci.plot2a, ci.plot2b, ci.plot2c, ci.plot3a, ci.plot3b, ci.plot3c))
colnames(out.mat) <- c("CI", "Y", "Low", "High")
out.mat$DV <- rep(c("A. Trustworthiness of Iran", "B. Danger posed by Iran", "C. Support for deal"), each=3*length(c.val))
out.mat$Mod <- rep(rep(c("Inspections: None", "Inspections: Civilian Only", "Inspections: All Sites"), each=length(c.val)),3)
out.mat$Mod <- as.factor(out.mat$Mod)
out.mat$Mod <- relevel(out.mat$Mod, ref="Inspections: Civilian Only")
out.mat$Mod <- relevel(out.mat$Mod, ref="Inspections: None")

dev.new(height=7, width=7.2)
ggplot(out.mat, aes(x=CI, y=Y)) + geom_ribbon(aes(ymin=Low, ymax=High), fill="grey70", alpha=0.5) + geom_line(aes(x=CI, y=Y)) + geom_hline(yintercept=0, linetype=3, alpha=0.5) +facet_grid(DV ~ Mod) + labs(x="Cooperative internationalism", y="Conditional effect of dismantling fuel cycle") + theme_bw()

### Table 3: Results robust to controlling for heterogeneous information

mod.info1 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All)*ci1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + know1 + ideo1, data=dat1)
mod.info2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All)*ci1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + know1 + ideo1, data=dat1)
mod.info3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All)*ci1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + Male + log(age) + White + educ + know1 + ideo1, data=dat1)

stargazer(mod.info1, mod.info2, mod.info3,  title="Results robust to controlling for heterogenous information", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:know", covariate.labels=c("Inspections: None", "Inspections: All", "Cooperative internationalism", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Militant internationalism", "Isolationism", "Male", "log(Age)", "White", "Education", "Knowledge about Iran", "Ideology", "Inspections: None x CI", "Inspections: All X CI", "Fuel Cycle: Continue x CI", "Fuel Cycle: Dismantle x CI", "Constant"), dep.var.labels.include=FALSE, column.labels=c("Trustworthy", "Dangerous", "Approval of Deal"))

#### Table 4: Conditional effects of signals by feelings towards Iran

mod.feel1 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All)*iran + (FuelCycle_Continue + FuelCycle_Dismantle)*iran + mi1 + iso1 + Male + log(age) + White + educ + ci1 + ideo1, data=dat1)
mod.feel2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All)*iran + (FuelCycle_Continue + FuelCycle_Dismantle)*iran + mi1 + iso1 + Male + log(age) + White + educ + ci1 + ideo1, data=dat1)
mod.feel3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All)*iran + (FuelCycle_Continue + FuelCycle_Dismantle)*iran + mi1 + iso1 + Male + log(age) + White + educ + ci1 + ideo1, data=dat1)

stargazer(mod.feel1, mod.feel2, mod.feel3,  title="Conditional effects of signals by feelings towards Iran", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:therm", covariate.labels=c("Inspections: None", "Inspections: All", "Feelings towards Iran", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Militant internationalism", "Isolationism", "Male", "log(Age)", "White", "Education", "Cooperative internationalism", "Ideology", "Inspections: None x Iran", "Inspections: All x Iran", "Fuel Cycle: Continue x Iran", "Fuel Cycle: Dismantle x Iran", "Constant"), dep.var.labels=c("Trustworthy", "Dangerous", "Support for deal"))


### Table 5: Results using political ideology as a moderator

mod.ideo1 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All)*ideo1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ideo1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.ideo2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All)*ideo1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ideo1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.ideo3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All)*ideo1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ideo1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)

stargazer(mod.ideo1, mod.ideo2, mod.ideo3,  title="Results using political ideology as a moderator", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:ideo", covariate.labels=c("Inspections: None", "Inspections: All", "Ideology", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Cooperative internationalism", "Militant internationalism", "Isolationism", "Male", "log(Age)", "White", "Education", "Party ID", "Inspections: None x Ideology", "Inspections: All X Ideology", "Fuel Cycle: Continue x Ideology", "Fuel Cycle: Dismantle x Ideology", "Constant"), dep.var.labels.include=FALSE, column.labels=c("Trustworthy", "Dangerous", "Approval of Deal"))

### Figure 3: Conditional effects of signals by political ideology

set.seed(43215)

ideo.mat1a <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*ideo1 + (FuelCycle_Freeze + FuelCycle_Dismantle) + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	ideo.mat1a[j,] <- coef(temp.mod)[c(3,16)]
}

ideo.mat1b <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All) + (FuelCycle_Freeze + FuelCycle_Dismantle)*ideo1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	ideo.mat1b[j,] <- coef(temp.mod)[c(5,16)]
}

ideo.mat2a <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*ideo1 + (FuelCycle_Freeze + FuelCycle_Dismantle) + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	ideo.mat2a[j,] <- coef(temp.mod)[c(3,16)]
}

ideo.mat2b <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All) + (FuelCycle_Freeze + FuelCycle_Dismantle)*ideo1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	ideo.mat2b[j,] <- coef(temp.mod)[c(5,16)]
}

ideo.mat3a <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*ideo1 + (FuelCycle_Freeze + FuelCycle_Dismantle) + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	ideo.mat3a[j,] <- coef(temp.mod)[c(3,16)]
}

ideo.mat3b <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All) + (FuelCycle_Freeze + FuelCycle_Dismantle)*ideo1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	ideo.mat3b[j,] <- coef(temp.mod)[c(5,16)]
}

c.val <- seq(0,1,length.out=11)
ideo.plot1a <- matrix(NA, 11,3)
for (i in 1:11){
	ideo.plot1a[i,1] <- mean(ideo.mat1a[,1] + c.val[i]*ideo.mat1a[,2])
	ideo.plot1a[i,2] <- quantile(ideo.mat1a[,1] + c.val[i]*ideo.mat1a[,2], 0.025)
	ideo.plot1a[i,3] <- quantile(ideo.mat1a[,1] + c.val[i]*ideo.mat1a[,2], 0.975)  
}

ideo.plot1b <- matrix(NA, 11,3)
for (i in 1:11){
	ideo.plot1b[i,1] <- mean(ideo.mat1b[,1] + c.val[i]*ideo.mat1b[,2])
	ideo.plot1b[i,2] <- quantile(ideo.mat1b[,1] + c.val[i]*ideo.mat1b[,2], 0.025)
	ideo.plot1b[i,3] <- quantile(ideo.mat1b[,1] + c.val[i]*ideo.mat1b[,2], 0.975)  
}

ideo.plot2a <- matrix(NA, 11,3)
for (i in 1:11){
	ideo.plot2a[i,1] <- mean(ideo.mat2a[,1] + c.val[i]*ideo.mat2a[,2])
	ideo.plot2a[i,2] <- quantile(ideo.mat2a[,1] + c.val[i]*ideo.mat2a[,2], 0.025)
	ideo.plot2a[i,3] <- quantile(ideo.mat2a[,1] + c.val[i]*ideo.mat2a[,2], 0.975)  
}

ideo.plot2b <- matrix(NA, 11,3)
for (i in 1:11){
	ideo.plot2b[i,1] <- mean(ideo.mat2b[,1] + c.val[i]*ideo.mat2b[,2])
	ideo.plot2b[i,2] <- quantile(ideo.mat2b[,1] + c.val[i]*ideo.mat2b[,2], 0.025)
	ideo.plot2b[i,3] <- quantile(ideo.mat2b[,1] + c.val[i]*ideo.mat2b[,2], 0.975)  
}

ideo.plot3a <- matrix(NA, 11,3)
for (i in 1:11){
	ideo.plot3a[i,1] <- mean(ideo.mat3a[,1] + c.val[i]*ideo.mat3a[,2])
	ideo.plot3a[i,2] <- quantile(ideo.mat3a[,1] + c.val[i]*ideo.mat3a[,2], 0.025)
	ideo.plot3a[i,3] <- quantile(ideo.mat3a[,1] + c.val[i]*ideo.mat3a[,2], 0.975)  
}

ideo.plot3b <- matrix(NA, 11,3)
for (i in 1:11){
	ideo.plot3b[i,1] <- mean(ideo.mat3b[,1] + c.val[i]*ideo.mat3b[,2])
	ideo.plot3b[i,2] <- quantile(ideo.mat3b[,1] + c.val[i]*ideo.mat3b[,2], 0.025)
	ideo.plot3b[i,3] <- quantile(ideo.mat3b[,1] + c.val[i]*ideo.mat3b[,2], 0.975)  
}

dev.new(height=4,width=4)
par(mar=c(4.1,4.1,1.1,1.1), oma=c(0,0,0,0))

plot(c.val, ideo.plot1a[,1], type="n", xlab="Ideology", ylab="Conditional effect of complete inspections", main="", ylim=c(-2,4))
polygon(c(c.val, rev(c.val)), c(ideo.plot1a[,2], rev(ideo.plot1a[,3])), col="grey")
lines(c.val, ideo.plot1a[,1], lwd=2)
abline(h=0, lty=3)

plot(c.val, ideo.plot1b[,1], type="n", xlab="Ideology", ylab="Conditional effect of dismantling fuel cycle", main=" ", ylim=c(-2,4))
polygon(c(c.val, rev(c.val)), c(ideo.plot1b[,2], rev(ideo.plot1b[,3])), col="grey")
lines(c.val, ideo.plot1b[,1], lwd=2)
abline(h=0, lty=3)

plot(c.val, ideo.plot2a[,1], type="n", xlab="Ideology", ylab="Conditional effect of complete inspections", main="", ylim=c(-3,2))
polygon(c(c.val, rev(c.val)), c(ideo.plot2a[,2], rev(ideo.plot2a[,3])), col="grey")
lines(c.val, ideo.plot2a[,1], lwd=2)
abline(h=0, lty=3)

plot(c.val, ideo.plot2b[,1], type="n", xlab="Ideology", ylab="Conditional effect of dismantling fuel cycle", main="", ylim=c(-3,2))
polygon(c(c.val, rev(c.val)), c(ideo.plot2b[,2], rev(ideo.plot2b[,3])), col="grey")
lines(c.val, ideo.plot2b[,1], lwd=2)
abline(h=0, lty=3)

plot(c.val, ideo.plot3a[,1], type="n", xlab="Ideology", ylab="Conditional effect of complete inspections", main="", ylim=c(-1,3))
polygon(c(c.val, rev(c.val)), c(ideo.plot3a[,2], rev(ideo.plot3a[,3])), col="grey")
lines(c.val, ideo.plot3a[,1], lwd=2)
abline(h=0, lty=3)

plot(c.val, ideo.plot3b[,1], type="n", xlab="Ideology", ylab="Conditional effect of dismantling fuel cycle", main="", ylim=c(-1,3))
polygon(c(c.val, rev(c.val)), c(ideo.plot3b[,2], rev(ideo.plot3b[,3])), col="grey")
lines(c.val, ideo.plot3b[,1], lwd=2)
abline(h=0, lty=3)

#### Table 6: Results using militant internationalism as moderator

mod.mi1 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All)*mi1 + (FuelCycle_Continue + FuelCycle_Dismantle)*mi1 + ci1 + ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.mi2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All)*mi1 + (FuelCycle_Continue + FuelCycle_Dismantle)*mi1 + ci1 + ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.mi3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All)*mi1 + (FuelCycle_Continue + FuelCycle_Dismantle)*mi1 + ci1 + ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)

stargazer(mod.mi1, mod.mi2, mod.mi3,  title="Results using militant internationalism as a moderator", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:mi", covariate.labels=c("Inspections: None", "Inspections: All", "Militant internationalism", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Cooperative internationalism", "Ideology", "Isolationism", "Male", "log(Age)", "White", "Education", "Party ID", "Inspections: None x MI", "Inspections: All X MI", "Fuel Cycle: Continue x MI", "Fuel Cycle: Dismantle x MI", "Constant"), dep.var.labels.include=FALSE, column.labels=c("Trustworthy", "Dangerous", "Approval of Deal"))

#### Figure 4: Conditional effects of signals by militant internationalism

set.seed(43215)

mi.mat1a <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle) + ci1 + ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	mi.mat1a[j,] <- coef(temp.mod)[c(3,16)]
}

mi.mat1b <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All) + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + ci1 +  ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	mi.mat1b[j,] <- coef(temp.mod)[c(5,16)]
}

mi.mat2a <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle) + ci1 +  ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	mi.mat2a[j,] <- coef(temp.mod)[c(3,16)]
}

mi.mat2b <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All) + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + ci1 +  ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	mi.mat2b[j,] <- coef(temp.mod)[c(5,16)]
}

mi.mat3a <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle) + ci1 + ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	mi.mat3a[j,] <- coef(temp.mod)[c(3,16)]
}

mi.mat3b <- matrix(NA, nrow=B,ncol=2)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All) + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 +ci1 +  ideo1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	mi.mat3b[j,] <- coef(temp.mod)[c(5,16)]
}


m.val <- seq(0,1,length.out=11)
mi.plot1a <- matrix(NA, 11,3)
for (i in 1:11){
	mi.plot1a[i,1] <- mean(mi.mat1a[,1] + m.val[i]*mi.mat1a[,2])
	mi.plot1a[i,2] <- quantile(mi.mat1a[,1] + m.val[i]*mi.mat1a[,2], 0.025)
	mi.plot1a[i,3] <- quantile(mi.mat1a[,1] + m.val[i]*mi.mat1a[,2], 0.975)  
}

mi.plot1b <- matrix(NA, 11,3)
for (i in 1:11){
	mi.plot1b[i,1] <- mean(mi.mat1b[,1] + m.val[i]*mi.mat1b[,2])
	mi.plot1b[i,2] <- quantile(mi.mat1b[,1] + m.val[i]*mi.mat1b[,2], 0.025)
	mi.plot1b[i,3] <- quantile(mi.mat1b[,1] + m.val[i]*mi.mat1b[,2], 0.975)  
}

mi.plot2a <- matrix(NA, 11,3)
for (i in 1:11){
	mi.plot2a[i,1] <- mean(mi.mat2a[,1] + m.val[i]*mi.mat2a[,2])
	mi.plot2a[i,2] <- quantile(mi.mat2a[,1] + m.val[i]*mi.mat2a[,2], 0.025)
	mi.plot2a[i,3] <- quantile(mi.mat2a[,1] + m.val[i]*mi.mat2a[,2], 0.975)  
}

mi.plot2b <- matrix(NA, 11,3)
for (i in 1:11){
	mi.plot2b[i,1] <- mean(mi.mat2b[,1] + m.val[i]*mi.mat2b[,2])
	mi.plot2b[i,2] <- quantile(mi.mat2b[,1] + m.val[i]*mi.mat2b[,2], 0.025)
	mi.plot2b[i,3] <- quantile(mi.mat2b[,1] + m.val[i]*mi.mat2b[,2], 0.975)  
}

mi.plot3a <- matrix(NA, 11,3)
for (i in 1:11){
	mi.plot3a[i,1] <- mean(mi.mat3a[,1] + m.val[i]*mi.mat3a[,2])
	mi.plot3a[i,2] <- quantile(mi.mat3a[,1] + m.val[i]*mi.mat3a[,2], 0.025)
	mi.plot3a[i,3] <- quantile(mi.mat3a[,1] + m.val[i]*mi.mat3a[,2], 0.975)  
}

mi.plot3b <- matrix(NA, 11,3)
for (i in 1:11){
	mi.plot3b[i,1] <- mean(mi.mat3b[,1] + m.val[i]*mi.mat3b[,2])
	mi.plot3b[i,2] <- quantile(mi.mat3b[,1] + m.val[i]*mi.mat3b[,2], 0.025)
	mi.plot3b[i,3] <- quantile(mi.mat3b[,1] + m.val[i]*mi.mat3b[,2], 0.975)  
}


dev.new(height=4,width=4)
par(mar=c(4.1,4.1,1.1,1.1), oma=c(0,0,0,0))

plot(m.val, mi.plot1a[,1], type="n", xlab="Militant internationalism", ylab="Conditional effect of complete inspections", main="", ylim=c(-2,4))
polygon(c(m.val, rev(m.val)), c(mi.plot1a[,2], rev(mi.plot1a[,3])), col="grey")
lines(m.val, mi.plot1a[,1], lwd=2)
abline(h=0, lty=3)

plot(m.val, mi.plot1b[,1], type="n", xlab="Militant internationalism", ylab="Conditional effect of dismantling fuel cycle", main=" ", ylim=c(-2,4))
polygon(c(m.val, rev(m.val)), c(mi.plot1b[,2], rev(mi.plot1b[,3])), col="grey")
lines(m.val, mi.plot1b[,1], lwd=2)
abline(h=0, lty=3)

plot(m.val, mi.plot2a[,1], type="n", xlab="Militant internationalism", ylab="Conditional effect of complete inspections", main="", ylim=c(-3,2))
polygon(c(m.val, rev(m.val)), c(mi.plot2a[,2], rev(mi.plot2a[,3])), col="grey")
lines(m.val, mi.plot2a[,1], lwd=2)
abline(h=0, lty=3)

plot(m.val, mi.plot2b[,1], type="n", xlab="Militant internationalism", ylab="Conditional effect of dismantling fuel cycle", main="", ylim=c(-3,2))
polygon(c(m.val, rev(m.val)), c(mi.plot2b[,2], rev(mi.plot2b[,3])), col="grey")
lines(m.val, mi.plot2b[,1], lwd=2)
abline(h=0, lty=3)

plot(m.val, mi.plot3a[,1], type="n", xlab="Militant internationalism", ylab="Conditional effect of complete inspections", main="", ylim=c(-1,3))
polygon(c(m.val, rev(m.val)), c(mi.plot3a[,2], rev(mi.plot3a[,3])), col="grey")
lines(m.val, mi.plot3a[,1], lwd=2)
abline(h=0, lty=3)

plot(m.val, mi.plot3b[,1], type="n", xlab="Militant internationalism", ylab="Conditional effect of dismantling fuel cycle", main="", ylim=c(-1,3))
polygon(c(m.val, rev(m.val)), c(mi.plot3b[,2], rev(mi.plot3b[,3])), col="grey")
lines(m.val, mi.plot3b[,1], lwd=2)
abline(h=0, lty=3)

#### Table 7: Trustworthiness of Iran (controlling for partisanship)

mod.t1 <- lm(trustworthy ~ Inspection_NotAllow + Inspection_All + FuelCycle_Continue + FuelCycle_Dismantle, data=dat1)
mod.t2 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.t3 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + Male + log(age) + White + educ + pid1, data=dat1)
mod.t4 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)

stargazer(mod.t1, mod.t2, mod.t3, mod.t4,  title="Trustworthiness of Iran", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:trust1", covariate.labels=c("Inspections: None", "Inspections: All", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Militant internationalism", "Cooperative internationalism", "Isolationism", "Feelings towards Iran", "Need for cognition", "Male", "log(Age)", "White", "Education", "Party ID", "Constant"))

##### Table 8: CI moderates the impact of costly signals (controlling for partisanship)

mod.ci1 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All)*ci1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.ci2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All)*ci1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + iran + cognition1+ Male + log(age) + White + educ + pid1, data=dat1)
mod.ci3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All)*ci1 + (FuelCycle_Continue + FuelCycle_Dismantle)*ci1 + mi1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)

stargazer(mod.ci1, mod.ci2, mod.ci3,  title="CI moderates the impact of costly signals", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:ci1", covariate.labels=c("Inspections: None", "Inspections: All", "Cooperative internationalism", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Militant internationalism", "Isolationism", "Feelings towards Iran", "Need for cognition", "Male", "log(Age)", "White", "Education", "Party ID", "Inspections: None x CI", "Inspections: All x CI", "Fuel Cycle: Continue x CI", "Fuel Cycle: Dismantle x CI", "Constant"))


##### Table 9: Partisanship moderates the impact of costly signals

mod.pid1 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All)*pid1 + (FuelCycle_Continue + FuelCycle_Dismantle)*pid1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=dat1)
summary(mod.pid1)

mod.pid2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All)*pid1 + (FuelCycle_Continue + FuelCycle_Dismantle)*pid1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=dat1)
summary(mod.pid2)

mod.pid3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All)*pid1 + (FuelCycle_Continue + FuelCycle_Dismantle)*pid1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + ideo1, data=dat1)
summary(mod.pid3)

stargazer(mod.pid1, mod.pid2, mod.pid3,  title="Partisanship moderates the impact of costly signals", omit.stat=c("LL", "ser", "f"), style="apsr", digits=3, label="tab:pid", covariate.labels=c("Inspections: None", "Inspections: All", "Party ID", "Fuel Cycle: Continue", "Fuel Cycle: Dismantle", "Cooperative internationalism", "Militant internationalism", "Isolationism", "Male", "log(Age)", "White", "Education", "Ideology", "Inspections: None x Party ID", "Inspections: All x Party ID", "Fuel Cycle: Continue x Party ID", "Fuel Cycle: Dismantle x Party ID", "Constant"))

###### Figure 5: Partisanship moderates the impact of costly signals
set.seed(43215)
B <- 1500

pid2.mat1a <- matrix(NA, nrow=B,ncol=4)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	pid2.mat1a[j,] <- coef(temp.mod)[c(3,15,6,17)]
}

pid2.mat2a <- matrix(NA, nrow=B,ncol=4)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	pid2.mat2a[j,] <- coef(temp.mod)[c(3,15,6,17)]
}

pid2.mat3a <- matrix(NA, nrow=B,ncol=4)
for (j in 1:B){
	k <- sample(1:nrow(dat1),nrow(dat1),replace=TRUE)
	temp <- dat1[k,]
	temp.mod <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + ci1 + mi1 + iso1 + Male + log(age) + White + educ + pid1, data=temp)
	pid2.mat3a[j,] <- coef(temp.mod)[c(3,15,6,17)]
}

p.val <- seq(0,1,length.out=7)
pid.plot1a <- matrix(NA,7,3)
for (i in 1:nrow(pid.plot1a)){
	pid.plot1a[i,1] <- mean(pid2.mat1a[,1] + p.val[i]*pid2.mat1a[,2])
	pid.plot1a[i,2] <- quantile(pid2.mat1a[,1] + p.val[i]*pid2.mat1a[,2], 0.025)
	pid.plot1a[i,3] <- quantile(pid2.mat1a[,1] + p.val[i]*pid2.mat1a[,2], 0.975)  
}

pid.plot2a <- matrix(NA,7,3)
for (i in 1:nrow(pid.plot2a)){
	pid.plot2a[i,1] <- mean(pid2.mat2a[,1] + p.val[i]*pid2.mat2a[,2])
	pid.plot2a[i,2] <- quantile(pid2.mat2a[,1] + p.val[i]*pid2.mat2a[,2], 0.025)
	pid.plot2a[i,3] <- quantile(pid2.mat2a[,1] + p.val[i]*pid2.mat2a[,2], 0.975)  
}

pid.plot3a <- matrix(NA,7,3)
for (i in 1:nrow(pid.plot3a)){
	pid.plot3a[i,1] <- mean(pid2.mat3a[,1] + p.val[i]*pid2.mat3a[,2])
	pid.plot3a[i,2] <- quantile(pid2.mat3a[,1] + p.val[i]*pid2.mat3a[,2], 0.025)
	pid.plot3a[i,3] <- quantile(pid2.mat3a[,1] + p.val[i]*pid2.mat3a[,2], 0.975)  
}

pid.plot1b <- matrix(NA,7,3)
for (i in 1:nrow(pid.plot1b)){
	pid.plot1b[i,1] <- mean(pid2.mat1a[,3] + p.val[i]*pid2.mat1a[,4])
	pid.plot1b[i,2] <- quantile(pid2.mat1a[,3] + p.val[i]*pid2.mat1a[,4], 0.025)
	pid.plot1b[i,3] <- quantile(pid2.mat1a[,3] + p.val[i]*pid2.mat1a[,4], 0.975)  
}

pid.plot2b <- matrix(NA,7,3)
for (i in 1:nrow(pid.plot2b)){
	pid.plot2b[i,1] <- mean(pid2.mat2a[,3] + p.val[i]*pid2.mat2a[,4])
	pid.plot2b[i,2] <- quantile(pid2.mat2a[,3] + p.val[i]*pid2.mat2a[,4], 0.025)
	pid.plot2b[i,3] <- quantile(pid2.mat2a[,3] + p.val[i]*pid2.mat2a[,4], 0.975)  
}

pid.plot3b <- matrix(NA,7,3)
for (i in 1:nrow(pid.plot3b)){
	pid.plot3b[i,1] <- mean(pid2.mat3a[,3] + p.val[i]*pid2.mat3a[,4])
	pid.plot3b[i,2] <- quantile(pid2.mat3a[,3] + p.val[i]*pid2.mat3a[,4], 0.025)
	pid.plot3b[i,3] <- quantile(pid2.mat3a[,3] + p.val[i]*pid2.mat3a[,4], 0.975)  
}


dev.new(height=4,width=4)
par(mar=c(4.1,4.1,1.1,1.1), oma=c(0,0,0,0))

plot(p.val, pid.plot1a[,1], type="n", xlab="Party ID", ylab="Conditional effect of complete inspections on trust", main="", ylim=c(-2,4))
polygon(c(p.val, rev(p.val)), c(pid.plot1a[,2], rev(pid.plot1a[,3])), col="grey")
lines(p.val, pid.plot1a[,1], lwd=2)
abline(h=0, lty=3)

plot(p.val, pid.plot1b[,1], type="n", xlab="Party ID", ylab="Conditional effect of dismantling fuel cycle on trust", main=" ", ylim=c(-2,4))
polygon(c(p.val, rev(p.val)), c(pid.plot1b[,2], rev(pid.plot1b[,3])), col="grey")
lines(p.val, pid.plot1b[,1], lwd=2)
abline(h=0, lty=3)

plot(p.val, pid.plot2a[,1], type="n", xlab="Party ID", ylab="Conditional effect of complete inspections on danger", main="", ylim=c(-3,2))
polygon(c(p.val, rev(p.val)), c(pid.plot2a[,2], rev(pid.plot2a[,3])), col="grey")
lines(p.val, pid.plot2a[,1], lwd=2)
abline(h=0, lty=3)

plot(p.val, pid.plot2b[,1], type="n", xlab="Party ID", ylab="Conditional effect of dismantling fuel cycle on danger", main="", ylim=c(-3,2))
polygon(c(p.val, rev(p.val)), c(pid.plot2b[,2], rev(pid.plot2b[,3])), col="grey")
lines(p.val, pid.plot2b[,1], lwd=2)
abline(h=0, lty=3)

plot(p.val, pid.plot3a[,1], type="n", xlab="Party ID", ylab="Conditional effect of complete inspections on support", main="", ylim=c(-1,3))
polygon(c(p.val, rev(p.val)), c(pid.plot3a[,2], rev(pid.plot3a[,3])), col="grey")
lines(p.val, pid.plot3a[,1], lwd=2)
abline(h=0, lty=3)

plot(p.val, pid.plot3b[,1], type="n", xlab="Party ID", ylab="Conditional effect of dismantling fuel cycle on support", main="", ylim=c(-1,3))
polygon(c(p.val, rev(p.val)), c(pid.plot3b[,2], rev(pid.plot3b[,3])), col="grey")
lines(p.val, pid.plot3b[,1], lwd=2)
abline(h=0, lty=3)

##### Figure 6: Foreign policy orientations trump partisanship

#A series of regression models for our three dependent variables of interest, replicating Appendix 2 Table 7, and Appendix 2 Table 1

mod.t1 <- lm(trustworthy ~ Inspection_NotAllow + Inspection_All + FuelCycle_Continue + FuelCycle_Dismantle, data=dat1)
mod.t2 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.t3 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + Male + log(age) + White + educ + pid1, data=dat1)
mod.t4 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.t5 <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*ci1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.t6 <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.t7 <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)

mod.d1 <- lm(dangerous ~ Inspection_NotAllow + Inspection_All + FuelCycle_Continue + FuelCycle_Dismantle, data=dat1)
mod.d2 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.d3 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + Male + log(age) + White + educ + pid1, data=dat1)
mod.d4 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.d5 <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*ci1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.d6 <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.d7 <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)

mod.s1 <- lm(dealSupport ~ Inspection_NotAllow + Inspection_All + FuelCycle_Continue + FuelCycle_Dismantle, data=dat1)
mod.s2 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.s3 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + Male + log(age) + White + educ + pid1, data=dat1)
mod.s4 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.s5 <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*ci1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.s6 <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
mod.s7 <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=dat1)
 
## Panel a: coefficient plot 
df1 <- matrix(NA, nrow=27, ncol=3)
getSE <- function(mod, var_name){
	i <- which(names(mod$coef)==var_name)
	b <- mod$coef[i]
	se <- sqrt(vcov(mod)[i,i])
	return(c(b,b-1.96*se,b+1.96*se))
}

df1[1,] <- getSE(mod.t4, "ci1")
df1[2,] <- getSE(mod.t4, "mi1")
df1[3,] <- getSE(mod.t4, "pid1")
df1[4,] <- getSE(mod.d4, "ci1")
df1[5,] <- getSE(mod.d4, "mi1")
df1[6,] <- getSE(mod.d4, "pid1")
df1[7,] <- getSE(mod.s4, "ci1")
df1[8,] <- getSE(mod.s4, "mi1")
df1[9,] <- getSE(mod.s4, "pid1")
df1[10,] <- getSE(mod.t5, "Inspection_All:ci1")
df1[11,] <- getSE(mod.t6, "Inspection_All:mi1")
df1[12,] <- getSE(mod.t7, "Inspection_All:pid1")
df1[13,] <- getSE(mod.d5, "Inspection_All:ci1")
df1[14,] <- getSE(mod.d6, "Inspection_All:mi1")
df1[15,] <- getSE(mod.d7, "Inspection_All:pid1")
df1[16,] <- getSE(mod.s5, "Inspection_All:ci1")
df1[17,] <- getSE(mod.s6, "Inspection_All:mi1")
df1[18,] <- getSE(mod.s7, "Inspection_All:pid1")
df1[19,] <- getSE(mod.t5, "ci1:FuelCycle_Dismantle")
df1[20,] <- getSE(mod.t6, "mi1:FuelCycle_Dismantle")
df1[21,] <- getSE(mod.t7, "pid1:FuelCycle_Dismantle")
df1[22,] <- getSE(mod.d5, "ci1:FuelCycle_Dismantle")
df1[23,] <- getSE(mod.d6, "mi1:FuelCycle_Dismantle")
df1[24,] <- getSE(mod.d7, "pid1:FuelCycle_Dismantle")
df1[25,] <- getSE(mod.s5, "ci1:FuelCycle_Dismantle")
df1[26,] <- getSE(mod.s6, "mi1:FuelCycle_Dismantle")
df1[27,] <- getSE(mod.s7, "pid1:FuelCycle_Dismantle")

df1 <- as.data.frame(df1)
colnames(df1) <- c("Effect", "Low", "High")
df1$Variable.Name <- c("CI", "MI", "Party ID")
df1$DV.Name <- rep(c("Trustworthy", "Dangerous", "Support for Deal"), each=3)
df1$Variable.Name[10:18] <- paste(df1$Variable.Name[10:18], "x Inspections")
df1$Variable.Name[19:27] <- paste(df1$Variable.Name[19:27], "x Fuel Cycle")
df1$Effect.Type <- c(rep("Main Effect",9),rep("Conditional Effect: Inspections",9), rep("Conditional Effect: Fuel Cycle",9))
df1$Variable.Name2 <- c("CI", "MI", "Party ID")

df1$DV.Name <- as.factor(df1$DV.Name)
df1$DV.Name <- relevel(df1$DV.Name, ref="Trustworthy")
df1$Effect.Type <- as.factor(df1$Effect.Type)
df1$Effect.Type <- relevel(df1$Effect.Type, ref="Conditional Effect: Inspections")
df1$Effect.Type <- relevel(df1$Effect.Type, ref="Main Effect")

#Plot figure
ggplot(df1, aes(x=Variable.Name2, y=Effect)) + geom_point() + geom_pointrange(aes(ymin=Low, ymax=High)) + facet_grid(DV.Name ~ Effect.Type) + geom_hline(aes(yintercept=0), lty=3) + labs(y="Effect", x="Variable") + theme_bw()
 
## Panel b: violin plot of bootstrapped ratio of coefficient estimates
B <- 1500
set.seed(43215)

temp.mat <- matrix(NA, nrow=B, ncol=18)
for (i in 1:B){
	j <- sample(seq_len(nrow(dat1)), nrow(dat1), replace=TRUE)
	temp <- dat1[j,]
	mod.t4 <- lm(trustworthy ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
	temp.mat[i,1] <- coef(mod.t4)[which(names(mod.t4$coef)=="ci1")]/coef(mod.t4)[which(names(mod.t4$coef)=="pid1")]
	temp.mat[i,2] <- coef(mod.t4)[which(names(mod.t4$coef)=="mi1")]/coef(mod.t4)[which(names(mod.t4$coef)=="pid1")]	
	mod.d4 <- lm(dangerous ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
	temp.mat[i,3] <- coef(mod.d4)[which(names(mod.d4$coef)=="ci1")]/coef(mod.d4)[which(names(mod.d4$coef)=="pid1")]
	temp.mat[i,4] <- coef(mod.d4)[which(names(mod.d4$coef)=="mi1")]/coef(mod.d4)[which(names(mod.d4$coef)=="pid1")]
	mod.s4 <- lm(dealSupport ~ (Inspection_NotAllow + Inspection_All) + (FuelCycle_Continue + FuelCycle_Dismantle) + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
	temp.mat[i,5] <- coef(mod.s4)[which(names(mod.s4$coef)=="ci1")]/coef(mod.s4)[which(names(mod.s4$coef)=="pid1")]
	temp.mat[i,6] <- coef(mod.s4)[which(names(mod.s4$coef)=="mi1")]/coef(mod.s4)[which(names(mod.s4$coef)=="pid1")]
	mod.t5 <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*ci1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
	mod.t6 <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
	mod.t7 <- lm(trustworthy ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
mod.d5 <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*ci1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
mod.d6 <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
mod.d7 <- lm(dangerous ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
mod.s5 <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*ci1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*ci1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
mod.s6 <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*mi1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*mi1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
mod.s7 <- lm(dealSupport ~ (Inspection_NoMilitary + Inspection_All)*pid1 + (FuelCycle_Freeze + FuelCycle_Dismantle)*pid1 + mi1 + ci1 + iso1 + iran + cognition1 + Male + log(age) + White + educ + pid1, data=temp)
	temp.mat[i,7] <- coef(mod.t5)[which(names(mod.t5$coef)=="Inspection_All:ci1")]/coef(mod.t7)[which(names(mod.t7$coef)=="Inspection_All:pid1")]
	temp.mat[i,8] <- coef(mod.t6)[which(names(mod.t6$coef)=="Inspection_All:mi1")]/coef(mod.t7)[which(names(mod.t7$coef)=="Inspection_All:pid1")]
	temp.mat[i,9] <- coef(mod.d5)[which(names(mod.d5$coef)=="Inspection_All:ci1")]/coef(mod.d7)[which(names(mod.d7$coef)=="Inspection_All:pid1")]
	temp.mat[i,10] <-  coef(mod.d6)[which(names(mod.d6$coef)=="Inspection_All:mi1")]/coef(mod.d7)[which(names(mod.d7$coef)=="Inspection_All:pid1")]
	temp.mat[i,11] <- coef(mod.s5)[which(names(mod.s5$coef)=="Inspection_All:ci1")]/coef(mod.s7)[which(names(mod.s7$coef)=="Inspection_All:pid1")]
	temp.mat[i,12] <- coef(mod.s6)[which(names(mod.s6$coef)=="Inspection_All:mi1")]/coef(mod.s7)[which(names(mod.s7$coef)=="Inspection_All:pid1")]
	temp.mat[i,13] <- coef(mod.t5)[which(names(mod.t5$coef)=="ci1:FuelCycle_Dismantle")]/coef(mod.t7)[which(names(mod.t7$coef)=="pid1:FuelCycle_Dismantle")]
	temp.mat[i,14] <- coef(mod.t6)[which(names(mod.t6$coef)=="mi1:FuelCycle_Dismantle")]/coef(mod.t7)[which(names(mod.t7$coef)=="pid1:FuelCycle_Dismantle")]
	temp.mat[i,15] <- coef(mod.d5)[which(names(mod.d5$coef)=="ci1:FuelCycle_Dismantle")]/coef(mod.d7)[which(names(mod.d7$coef)=="pid1:FuelCycle_Dismantle")]
	temp.mat[i,16] <- coef(mod.d6)[which(names(mod.d6$coef)=="mi1:FuelCycle_Dismantle")]/coef(mod.d7)[which(names(mod.d7$coef)=="pid1:FuelCycle_Dismantle")]
	temp.mat[i,17] <- coef(mod.s5)[which(names(mod.s5$coef)=="ci1:FuelCycle_Dismantle")]/coef(mod.s7)[which(names(mod.s7$coef)=="pid1:FuelCycle_Dismantle")]
	temp.mat[i,18] <- coef(mod.s6)[which(names(mod.s6$coef)=="mi1:FuelCycle_Dismantle")]/coef(mod.s7)[which(names(mod.s7$coef)=="pid1:FuelCycle_Dismantle")]
}

df2 <- melt(temp.mat)
df2$Variable.Name <- rep(c("CI vs Party ID", "MI vs Party ID"), each=1500)
df2$DV.Name <- rep(c("Trustworthy", "Dangerous", "Support for Deal"), each=3000)
df2$Effect.Type <- c(rep("Main Effect",6*1500),rep("Conditional Effect: Inspections",6*1500), rep("Conditional Effect: Fuel Cycle",6*1500))

df2$DV.Name <- as.factor(df2$DV.Name)
df2$DV.Name <- relevel(df2$DV.Name, ref="Trustworthy")
df2$Effect.Type <- as.factor(df2$Effect.Type)
df2$Effect.Type <- relevel(df2$Effect.Type, ref="Conditional Effect: Inspections")
df2$Effect.Type <- relevel(df2$Effect.Type, ref="Main Effect")

#Generate plot; constrain y axis for purposes of legibility
ggplot(df2, aes(x=Variable.Name, y=value)) + geom_violin(fill="grey") + facet_grid(DV.Name ~ Effect.Type) + ylim(c(-20,20)) + labs(y="Bootstrapped ratio of coefficient estimates", x="Comparison") + theme_bw() + geom_hline(aes(yintercept=0), lty=3) 

#Calculate quantities of interest
coef(mod.t4)[which(names(mod.t4$coef)=="ci1")]/coef(mod.t4)[which(names(mod.t4$coef)=="pid1")] #CI vs Party ID for trustworthiness: -6.1
coef(mod.d4)[which(names(mod.d4$coef)=="mi1")]/coef(mod.d4)[which(names(mod.d4$coef)=="pid1")] #MI vs Party ID for dangerousness: 11.5
coef(mod.s4)[which(names(mod.s4$coef)=="ci1")]/coef(mod.s4)[which(names(mod.s4$coef)=="pid1")] #CI vs Party ID for support: -5.4


##### Table 10: Change in perceived trustworthiness following costly signals

mod.10a <- lm(trustDiff ~ complied + pledge, data=dat2)
mod.10b <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1, data=dat2)
mod.10c <- lm(trustDiff ~ complied + pledge + ci1 + mi1 + iran1 + news_iran1, data=dat2)
mod.10d <- lm(trustDiff ~ complied + pledge + ci1 + mi1 + iran1 + news_iran1 + iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2)

stargazer(mod.10a, mod.10b, mod.10c, mod.10d, title="Change in perceived trustworthiness following costly signals", style="apsr", digits=3, label="tab:trustDelta1", omit.stat=c("LL", "ser", "f"), covariate.labels=c("Complied condition", "Pledge condition", "CI", "MI", "Iran feeling", "Follows news about Iran deal", "Isolationism", "Knowledge", "Need for closure", "Ideology", "Education", "Male", "Intercept"), dep.var.labels.include=FALSE)

##### Table 11: Change in trustworthiness folowing each type of costly signal

#First, subset by treatment condition
dat2a <- subset(dat2, dat2$complied==1)
dat2b <- subset(dat2, dat2$ballistic==1)
dat2c <- subset(dat2, dat2$pledge==1)

mod.11a0 <- lm(trustDiff ~ 1, data=dat2a)
mod.11a1 <- lm(trustDiff ~ 1 + ci1 + mi1 + iran1 + news_iran1, data=dat2a)
mod.11a2 <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1 +iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2a)
mod.11b0 <- lm(trustDiff ~ 1, data=dat2b)
mod.11b1 <- lm(trustDiff ~ 1 + ci1 + mi1 + iran1 + news_iran1, data=dat2b)
mod.11b2 <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1 + iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2b)
mod.11c0 <- lm(trustDiff ~ 1, data=dat2c)
mod.11c1 <- lm(trustDiff ~ 1 + ci1 + mi1 + iran1 + news_iran1, data=dat2c)
mod.11c2 <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1 + iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2c)

stargazer(mod.11a0, mod.11a1, mod.11a2, mod.11b0, mod.11b1, mod.11b2, mod.11c0, mod.11c1, mod.11c2, title="Change in trustworthiness following each type of costly signal", style="apsr", digits=3, label="tab:trustDelta", omit.stat=c("LL", "ser", "f"), covariate.labels=c("CI", "MI", "Iran feeling", "Follows news about Iran deal", "Isolationism", "Knowledge", "Need for closure", "Ideology", "Education", "Male", "Intercept"), dep.var.labels.include=FALSE, column.labels=c("Compliance", "Ballistic", "Pledge"), column.separate=c(3,3,3))

##### Testing for variance of priors using thought listings

library(tidyverse)
library(tidytext)
library(glue)
library(stringr)

## Create new dataframe with just thoughts
#Find location of first and last thought variables
loc <- which(colnames(dat2)=="thought1" | colnames(dat2)=="thought7")
#Create thought dataframe
t.df <- data.frame(obs=1:nrow(dat2), dat2[,loc[1]:loc[2]])
t.df2 <- gather(t.df, key=entry, value=text, thought1:thought7) #Melt thoughts, preserving structure otherwise

token.df <- t.df2 %>% unnest_tokens(word,text) #Tokenize

### First, use Bing dictionary
s.df <- token.df %>% inner_join(get_sentiments("bing")) %>% count(entry, index=obs, sentiment) %>%
spread(sentiment, n, fill=0) %>% mutate(sentiment = positive - negative) #Sentiment dataframe, using Bing

#Now create new variables calculating the total sentiment score, plus the variance, for each respondent
s.df2 <- group_by(s.df, index) %>% summarize(Sum=sum(sentiment, na.rm=TRUE), Var=var(sentiment, na.rm=TRUE))

#Validate measures: do these make sense?
dat2$sumSent <- dat2$varSent <- NA
dat2$varSent[s.df2$index] <- s.df2$Var
dat2$sumSent[s.df2$index] <- s.df2$Sum

#Now validate it by comparing it with something we already think we know, like CI, Iran feeling thermometer, MI

cor(dat2$sumSent, dat2$ci1, use="complete.obs") #r=0.20
cor(dat2$sumSent, dat2$iran1, use="complete.obs") #r=0.12
cor(dat2$sumSent, dat2$mi1, use="complete.obs") #r=-0.07
#The measure is noisy, but everything seems to point in the right direction

cor(dat2$varSent, dat2$ci1, use="complete.obs") #r=0.05
cor(dat2$varSent, dat2$iran1, use="complete.obs") #r=0.001
cor(dat2$varSent, dat2$mi1, use="complete.obs") #r=-0.04
#So these orientations are unrelated to variance - this is good!

#### Table 12: Variance of priors (I)
mod.p1a0 <- lm(trustDiff ~ complied + pledge + varSent, data=dat2)
mod.p1a1 <- lm(trustDiff ~ complied + pledge + ci1 + mi1 + iran1 + news_iran1 + varSent, data=dat2)
mod.p1a2 <- lm(trustDiff ~ complied + pledge + ci1 + mi1 + iran1 + news_iran1 + varSent + iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2)
stargazer(mod.p1a0, mod.p1a1, mod.p1a2, title="Variance of priors (I)", style="apsr", digits=3, label="tab:trustVar", omit.stat=c("LL", "ser", "f"), covariate.labels=c("Complied condition", "Pledge condition", "CI", "MI", "Iran feeling", "Follows news about Iran deal", "Variance of priors", "Isolationism", "Knowledge", "Need for closure", "Ideology", "Education", "Male", "Intercept"), dep.var.labels.include=FALSE)

#### Table 13: Variance of priors (II)
dat2a <- subset(dat2, dat2$complied==1)
dat2b <- subset(dat2, dat2$ballistic==1)
dat2c <- subset(dat2, dat2$pledge==1)

mod.p2a0 <- lm(trustDiff ~ varSent, data=dat2a)
mod.p2a1 <- lm(trustDiff ~ 1 + ci1 + mi1 + iran1 + news_iran1 + varSent, data=dat2a)
mod.p2a2 <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1 + varSent +iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2a)

mod.p2b0 <- lm(trustDiff ~ varSent, data=dat2b)
mod.p2b1 <- lm(trustDiff ~ 1 + ci1 + mi1 + iran1 + news_iran1 + varSent, data=dat2b)
mod.p2b2 <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1 + varSent + iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2b)

mod.p2c0 <- lm(trustDiff ~ varSent, data=dat2c)
mod.p2c1 <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1 + varSent, data=dat2c)
mod.p2c2 <- lm(trustDiff ~ ci1 + mi1 + iran1 + news_iran1 + varSent + iso1 + know1 + closure1 + ideo1 + educ1 + Male, data=dat2c)

stargazer(mod.p2a0, mod.p2a1, mod.p2a2, mod.p2b0, mod.p2b1, mod.p2b2, mod.p2c0, mod.p2c1, mod.p2c2, title="Variance of priors (II)", style="apsr", digits=3, label="tab:trustVar2", omit.stat=c("LL", "ser", "f"), covariate.labels=c("CI", "MI", "Iran feeling", "News about Iran", "Variance of priors", "Isolationism", "Knowledge", "Need for closure", "Ideology", "Education", "Male", "Intercept"), dep.var.labels.include=FALSE, column.labels=c("Compliance", "Ballistic", "Pledge"), column.separate=c(3,3,3))

